﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using Chenxin.Lib.Utility;

public partial class IntentDetail : PageBase
{
    string strRecordId = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (string.IsNullOrEmpty(Request.Params["IntentionId"]))
            {
                Response.Redirect("~/page/errorpage.aspx?msg=非法信息", true);
            }
            if (!IsPostBack)
            {
                strRecordId = Request.Params["IntentionId"].ToString().Trim();
                this.hidRecordId.Value = strRecordId;
                Chenxin.Lib.Business.Entity.t_Intention_BaseInfo model = Chenxin.Lib.Business.DAL.t_Intention_BaseInfo.GetModel(new Guid(strRecordId));
                BindTable(model);
            }
        }
        catch (Exception ex)
        {
            Response.Redirect("~/page/errorpage.aspx?msg=" + Server.UrlEncode(ex.Message) + "", true);
        }
    }

    private void BindTable(Chenxin.Lib.Business.Entity.t_Intention_BaseInfo model)
    {
        StringBuilder sb = new StringBuilder();
        string status = "";
        switch (model.Status)
        {
            case -200:
                status = "未领用";
                break;
            case -100:
                status = "未使用";
                break;
            case -10:
                status = "未生效";
                break;
            case -40:
                status = "失效";
                break;
            case -20:
                status = "作废";
                break;
            case -30:
                status = "遗失";
                break;
            case 10:
                status = "生效";
                break;
        }

        //意向书基本信息
        sb.Append("<table width=\"100%\" cellspacing=\"1\" class=\"tabsorternew table_archive\">");
        sb.Append("<thead>");
        sb.Append("<tr>");
        sb.Append("<th colspan=\"8\" style='text-align:center;'><span>意向书基本信息</span></th>");
        sb.Append("</tr>");
        sb.Append("</thead>");
        sb.Append("<tbody>");
        sb.Append("<tr>");
        sb.AppendFormat("<th style=\"text-align:right;\">意向书编号：</th><td colspan=\"3\">{0}{4}</td><th style=\"text-align:right;\">意向书类型：</th><td>{1}<span style=\"color:Red;\">({2})</span></td><th style=\"text-align:right;\">成交编号：</th><td>{3}</td>", model.IntentionCode, model.IntentionType == 10 ? "买卖协议" : "居间协议", model.BusinessType == 10 ? "出售" : "出租", model.ContractCode.Length > 0 ? model.ContractCode : "--", "<span style=\"color:Red;\">(" + status + ")</span>");
        sb.Append("</tr>");
        sb.Append("<tr>");
        sb.AppendFormat("<th style=\"text-align:right;\">所属城市：</th><td>{0}</td><th style=\"text-align:right;\">所属公司：</th><td>{1}</td><th style=\"text-align:right;\">所属部门：</th><td>{2}</td><th style=\"text-align:right;\">所属秘书：</th><td>{3}</td>", model.BelongCityName, model.BelongCompany, model.BelongDepart, model.BelongEmp);
        sb.Append("</tr>");
        sb.Append("<tr>");
        sb.AppendFormat("<th style=\"text-align:right;\">下发时间：</th><td>{0}</td><th style=\"text-align:right;\">领取业务员：</th><td>{1}</td><th style=\"text-align:right;\">领取时间：</th><td>{2}</td><th style=\"text-align:right;\">换领时间：</th><td>{0}</td>", model.DeliverTime != null ? DateTime.Parse(model.DeliverTime.ToString()).ToShortDateString() : "--", model.LastEmployee.Length > 0 ? model.LastEmployee : "--", model.LastEmployeeGetTime != null ? DateTime.Parse(model.LastEmployeeGetTime.ToString()).ToShortDateString() : "--", model.ChangeTime != null ? DateTime.Parse(model.ChangeTime.ToString()).ToShortDateString() : "--");
        sb.Append("</tr>");
        sb.Append("<tr><td colspan=\"8\"  style=\"height:10px;\"><div class=\"clear\" style=\"height:10px\"></div></td></tr>");
        sb.Append("<tr>");
        sb.AppendFormat("<th style=\"text-align:right;\">房源编号：</th><td colspan=\"3\">{0}</td><th style=\"text-align:right;\">房源地址：</th><td colspan=\"3\">{1}</td>", model.HouseCode.Length > 0 ? model.HouseCode : "--", model.HouseAddress.Length > 0 ? model.HouseAddress : "--");
        sb.Append("</tr>");
        sb.Append("<tr>");
        sb.AppendFormat("<th style=\"text-align:right;\">所在区域：</th><td>{0}</td><th style=\"text-align:right;\">房屋总价：</th><td>{1}</td><th style=\"text-align:right;\">上家姓名：</th><td>{2}</td><th style=\"text-align:right;\">下家姓名：</th><td>{3}</td>", model.Region.Length > 0 ? model.Region : "--", model.TotalPrice.ToString().Length > 0 ? model.TotalPrice.ToString() : "--", model.CustomerName_Sale.Length > 0 ? model.CustomerName_Sale : "--", model.CustomerName_Buy.Length > 0 ? model.CustomerName_Buy : "--");
        sb.Append("</tr>");
        sb.Append("<tr>");
        sb.AppendFormat("<th style=\"text-align:right;\">总诚意金：</th><td>{1}</td><th style=\"text-align:right;\">首笔诚意金：</th><td>{0}</td><th style=\"text-align:right;\">已收诚意金：</th><td>{2}</td><th style=\"text-align:right;\">已转诚意金：</th><td>{3}</td>", model.FirstMoney.ToString().Length > 0 ? model.FirstMoney.ToString() : "--", model.TotalMoney.ToString().Length > 0 ? model.TotalMoney.ToString() : "--", model.GetedMoney.ToString().Length > 0 ? model.GetedMoney.ToString() : "--", model.DeliveredMoney.ToString().Length > 0 ? model.DeliveredMoney.ToString() : "--");
        sb.Append("</tr>");
        sb.Append("<tr>");
        sb.AppendFormat("<th style=\"text-align:right;\">约定截止签约日期：</th><td>{0}</td><th style=\"text-align:right;\">约定诚意金转移期限：</th><td>{1}</td><th style=\"text-align:right;\">约定诚意金转移方向：</th><td>{2}</td><th style=\"text-align:right;\">签呈收到时间：</th><td>{3}</td>", model.LastContractDate != null ? DateTime.Parse(model.LastContractDate.ToString()).ToShortDateString() : "--",model.LastDeliverMoneyDate != null ? DateTime.Parse(model.LastDeliverMoneyDate.ToString()).ToShortDateString() : "--",model.DeliverTo!=0?(model.DeliverTo==10?"房款":"定金"):"--",model.GetAuthorizeDate != null ? DateTime.Parse(model.GetAuthorizeDate.ToString()).ToShortDateString() : "--");
        sb.Append("</tr>");
        sb.Append("<tr>");
        sb.AppendFormat("<th style=\"text-align:right;\">遗失内容：</th><td colspan=\"7\">{0}</td>", model.LostItems.Length > 0 ? model.LostItems : "--");
        sb.Append("</tr>");
        sb.Append("<tr>");
        sb.AppendFormat("<th style=\"text-align:right;\">备注：</th><td colspan=\"7\">{0}</td>", model.StatusDescription.Length > 0 ? model.StatusDescription : "--");
        sb.Append("</tr>");
        sb.Append("</tbody>");
        sb.Append("</table>");
        sb.Append("<div class=\"clear\" style=\"height:20px\"></div>");

        //意向金收款明细
        sb.Append("<table width=\"100%\" cellspacing=\"1\"  class=\"tabsorternew table_archive\" >");
        sb.Append("<thead>");
        sb.Append("<tr><th style='text-align:center;'><span>收款记录明细</span></th></tr>");
        sb.Append("</thead>");
        sb.Append("</table>");
        sb.Append("<div style=\"overflow-x:scroll;height:auto; width:940px;\">");
        sb.Append("<table  width=\"1600px\">");
        sb.Append("<tbody>");
        sb.Append("<tr>");
        sb.AppendFormat("<th style=\"text-align:center;width:180px;\" class=\"th_show\">单据编号</th><th style=\"text-align:center;width:80px;\" class=\"th_show\">单据状态</th><th style=\"text-align:center;width:100px;\" class=\"th_show\">收款部门</th><th style=\"text-align:center;width:100px;\" class=\"th_show\">收款人员</th><th style=\"text-align:center;width:100px;\" class=\"th_show\">账户类型</th><th style=\"text-align:center;width:100px;\" class=\"th_show\">开单金额</th><th style=\"text-align:center;width:100px;\" class=\"th_show\">开单时间</th><th style=\"text-align:center;width:80px;\" class=\"th_show\">确认进账</th><th style=\"text-align:center;width:100px;\" class=\"th_show\">确认时间</th><th style=\"text-align:center;width:100px;\" class=\"th_show\">确认人</th>");
        sb.Append("</tr>");

        List<Chenxin.Lib.Business.Entity.t_Intention_BillGetedInfo> list = Chenxin.Lib.Business.DAL.t_Intention_BillGetedInfo.GetListArray("IntentionId='" + model.IntentionId + "' and InOrOut=1 ");
        if (list.Count > 0)
        {
            foreach (Chenxin.Lib.Business.Entity.t_Intention_BillGetedInfo info in list)
            {
                sb.Append("<tr>");
                sb.AppendFormat("<td class=\"td_show\">{0}</td><td class=\"td_show\">{1}</td><td class=\"td_show\">{2}</td><td class=\"td_show\">{3}</td><td class=\"td_show\">{4}</td><td class=\"td_show\">{5}</td><td class=\"td_show\">{6}</td><td class=\"td_show\">{7}</td><td class=\"td_show\">{8}</td><td class=\"td_show\">{9}</td>", info.BillCode, info.Status == 1 ? "<span style=\"color:green;\">有效</span>" : "<span style=\"color:Red;\">作废</span>", info.GetedDepart, info.GetedEmployee, info.AccountType == 10 ? "公司账户" : "授权账户", info.GetedMoney, DateTime.Parse(info.GetedTime.ToString()).ToShortDateString(), info.IsConfirmed.ToString().ToLower() == "true" ? "确认" : "否", info.ConfirmDate.ToString().Length > 0 ? DateTime.Parse(info.ConfirmDate.ToString()).ToShortDateString() : "", info.ConfirmEmployee);
                sb.Append("</tr>");
            }
        }
        sb.Append("</tbody>");
        sb.Append("</table>");
        sb.Append("</div>");
        sb.Append("<div class=\"clear\" style=\"height:20px\"></div>");

        //意向金转移明细
        sb.Append("<table width=\"100%\" cellspacing=\"1\"  class=\"tabsorternew table_archive\" >");
        sb.Append("<thead>");
        sb.Append("<tr><th style='text-align:center;'><span>转移记录明细</span></th></tr>");
        sb.Append("</thead>");
        sb.Append("</table>");
        sb.Append("<div style=\"overflow-x:scroll;height:auto; width:940px;\">");
        sb.Append("<table  width=\"1600px\">");
        sb.Append("<tbody>");
        sb.Append("<tr>");
        sb.AppendFormat("<th style=\"text-align:center;width:180px;\" class=\"th_show\">单据编号</th><th style=\"text-align:center;width:80px;\" class=\"th_show\">单据状态</th><th style=\"text-align:center;width:100px;\" class=\"th_show\">转移部门</th><th style=\"text-align:center;width:100px;\" class=\"th_show\">转移人员</th><th style=\"text-align:center;width:100px;\" class=\"th_show\">账户类型</th><th style=\"text-align:center;width:100px;\" class=\"th_show\">转移金额</th><th style=\"text-align:center;width:100px;\" class=\"th_show\">转移时间</th><th style=\"text-align:center;width:100px;\" class=\"th_show\">转移去向</th><th style=\"text-align:center;width:80px;\" class=\"th_show\">确认转移</th><th style=\"text-align:center;width:100px;\" class=\"th_show\">确认时间</th><th style=\"text-align:center;width:100px;\" class=\"th_show\">确认人</th><th style=\"text-align:center;width:100px;\" class=\"th_show\">代保管</th>");
        sb.Append("</tr>");

        List<Chenxin.Lib.Business.Entity.t_Intention_BillGetedInfo> list_pay = Chenxin.Lib.Business.DAL.t_Intention_BillGetedInfo.GetListArray("IntentionId='" + model.IntentionId + "' and InOrOut=0 ");
        if (list.Count > 0)
        {
            foreach (Chenxin.Lib.Business.Entity.t_Intention_BillGetedInfo info in list_pay)
            {
                string payto = "";
                switch (info.PayTo)
                {
                    case 10:
                        payto = "转定金";
                        break;
                    case 20:
                        payto = "转房款";
                        break;
                    case 30:
                        payto = "退还客户";
                        break;
                    case 40:
                        payto = "转佣金";
                        break;
                }
                string bCustody="";
                if(info.bCustody.ToString().Length>0)
                {
                    if(info.bCustody==10)
                        bCustody="是";
                    else
                        bCustody="否";
                }
                sb.Append("<tr>");
                sb.AppendFormat("<td class=\"td_show\">{0}</td><td class=\"td_show\">{1}</td><td class=\"td_show\">{2}</td><td class=\"td_show\">{3}</td><td class=\"td_show\">{4}</td><td class=\"td_show\">{5}</td><td class=\"td_show\">{6}</td><td class=\"td_show\">{7}</td><td class=\"td_show\">{8}</td><td class=\"td_show\">{9}</td><td class=\"td_show\">{10}</td><td class=\"td_show\">{11}</td>", info.BillCode, info.Status == 1 ? "<span style=\"color:green;\">有效</span>" : "<span style=\"color:Red;\">作废</span>", info.GetedDepart, info.GetedEmployee, info.AccountType == 10 ? "公司账户" : "授权账户", info.GetedMoney, DateTime.Parse(info.GetedTime.ToString()).ToShortDateString(), payto, info.IsConfirmed.ToString().ToLower() == "true" ? "确认" : "否", info.ConfirmDate.ToString().Length > 0 ? DateTime.Parse(info.ConfirmDate.ToString()).ToShortDateString() : "", info.ConfirmEmployee, bCustody);
                sb.Append("</tr>");
            }
        }
        sb.Append("</tbody>");
        sb.Append("</table>");
        sb.Append("</div>");
        sb.Append("<div class=\"clear\" style=\"height:20px\"></div>");

        //意向书添加意向金
        sb.Append("<table width=\"100%\" cellspacing=\"1\"  class=\"tabsorternew table_archive\" >");
        sb.Append("<thead>");
        sb.Append("<tr><th style='text-align:center;' colspan=\"3\" ><span>增加意向金记录</span></th></tr>");
        sb.Append("</thead>");
        sb.Append("<tbody>");
        sb.Append("<tr>");
        sb.AppendFormat("<th style=\"text-align:center;\" class=\"th_show\">增加金额</th><th style=\"text-align:center;\" class=\"th_show\">增加人</th><th style=\"text-align:center;\" class=\"th_show\">增加时间</th>");
        sb.Append("</tr>");
        List<Chenxin.Lib.Business.Entity.t_Intention_AddMoneyInfo> list_add = Chenxin.Lib.Business.DAL.t_Intention_AddMoneyInfo.GetListArray("IntentionId='" + model.IntentionId + "' ");
        if (list.Count > 0)
        {
            foreach (Chenxin.Lib.Business.Entity.t_Intention_AddMoneyInfo info in list_add)
            {
                sb.Append("<tr>");
                sb.AppendFormat("<td class=\"td_show\">{0}</td><td class=\"td_show\">{1}</td><td class=\"td_show\">{2}</td>",info.AddedMoney,info.AddEmployee,DateTime.Parse(info.AddTime.ToString()).ToShortDateString());
                sb.Append("</tr>");
            }
        }
        sb.Append("</tbody>");
        sb.Append("</table>");
        sb.Append("<div class=\"clear\" style=\"height:20px\"></div>");


        //意向书流程
        sb.Append("<table width=\"100%\" cellspacing=\"1\" class=\"tabsorternew table_archive\">");
        sb.Append("<thead>");
        sb.Append("<tr><th style='text-align:center;'><span>意向书流程</span></th></tr>");
        sb.Append("</thead>");
        sb.Append("</table>");
        sb.Append("<div>");
        sb.Append("<ul style=\"float: right; line-height: 18px; margin-top: 6px; margin-right: 5px;text-align:right; width:840px;\">");
        sb.Append("<li style=\"float: right; padding-right: 5px;\">回收</li>");
        sb.Append("<li style=\"float: right; padding-right: 5px;\"><div class=\"clsDiv clsCatalogB\" style=\"background-color:#f9a3ca;\"></div></li>");
        sb.Append("<li style=\"float: right; padding-right: 5px;\">变更</li>");
        sb.Append("<li style=\"float: right; padding-right: 5px;\"><div class=\"clsDiv clsCatalogB\" style=\"background-color:#f09254;\"></div></li>");
        sb.Append("<li style=\"float: right; padding-right: 5px;\">领用</li>");
        sb.Append("<li style=\"float: right; padding-right: 5px;\"><div class=\"clsDiv clsCatalogA\" style=\"background-color:#75cd02;\"></div></li>");
        sb.Append("<li style=\"float: right; padding-right: 5px;\">下发</li>");
        sb.Append("<li style=\"float: right; padding-right: 5px;\"><div class=\"clsDiv clsInvalid\" style=\"background-color:#86c6f7;\"></div></li>");        
        sb.Append("</ul>");
        sb.Append("</div>");
        sb.Append("<div style=\"height:auto; width:860px;margin:auto;\">");

        List<Chenxin.Lib.Business.Entity.t_Intention_DeliverLogs> logslist = Chenxin.Lib.Business.DAL.t_Intention_DeliverLogs.GetListArray(" intentionid='" + model.IntentionId + "' order by DeliverTime ");
        //下发：box_blue,领用：box_green,回收：box_pink,变更：box_organ
        for (int i = 0; i < logslist.Count; i++)
        {
            //初始流程：下发 
            if (i == 0)
            {
                sb.Append("<div id=\"box_blue\">");
                sb.Append("<div class=\"line0\"></div><div class=\"line1\"></div><div class=\"line2\"></div><div class=\"line3\"></div>");
                sb.AppendFormat("<div class=\"word\"><p>{0}</p><p>{1}/{2}</p></div>", DateTime.Parse(logslist[i].DeliverTime.ToString()).ToShortDateString(), logslist[i].DeliverDepart, logslist[i].DeliverEmp);
                sb.Append("<div class=\"line3\"></div><div class=\"line2\"></div><div class=\"line1\"></div><div class=\"line0\"></div>");
                sb.Append("</div>");
                sb.Append("<div class=\"arrow1\"></div>");

                sb.Append("<div id=\"box_blue\">");
                sb.Append("<div class=\"line0\"></div><div class=\"line1\"></div><div class=\"line2\"></div><div class=\"line3\"></div>");
                sb.AppendFormat("<div class=\"word\"><p>{0}</p><p>{1}/{2}</p></div>", DateTime.Parse(logslist[i].DeliverTime.ToString()).ToShortDateString(), logslist[i].ReceiveDepart, logslist[i].ReceiveEmp);
                sb.Append("<div class=\"line3\"></div><div class=\"line2\"></div><div class=\"line1\"></div><div class=\"line0\"></div>");
                sb.Append("</div>");
                sb.Append("<div class=\"arrow1\"></div>");
            }
            else
            {
                switch (logslist[i].Status)
                {
                    case 10:
                        sb.Append("<div id=\"box_blue\">");
                        break;
                    case 20:
                        sb.Append("<div id=\"box_green\">");
                        break;
                    case 30:
                        sb.Append("<div id=\"box_pink\">");
                        break;
                    case 40:
                        sb.Append("<div id=\"box_organ\">");
                        break;
                }
                sb.Append("<div class=\"line0\"></div><div class=\"line1\"></div><div class=\"line2\"></div><div class=\"line3\"></div>");
                sb.AppendFormat("<div class=\"word\"><p>{0}</p><p>{1}/{2}</p></div>", DateTime.Parse(logslist[i].DeliverTime.ToString()).ToShortDateString(), logslist[i].ReceiveDepart, logslist[i].ReceiveEmp);
                sb.Append("<div class=\"line3\"></div><div class=\"line2\"></div><div class=\"line1\"></div><div class=\"line0\"></div>");
                sb.Append("</div>");
                sb.Append("<div class=\"arrow1\"></div>");
            }
        }
        sb.Append("</div>");

        this.ltArchiveDetailInfo.Text = sb.ToString();
    }
}
